package ui.activites;

import ui.activites.R;

import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

/* 
 * TEMPORARY DEMO IMPLEMENTATION
 */

public class ScoresActivity extends BaseActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scores);
        
        TabHost host = (TabHost)findViewById(R.id.TabHost_Scores);
        host.setup();
        
        addTabSpecToHost(host, "localScoresTab", R.string.str_Local_Scores, R.id.ScrollViewLocalScores);
        addTabSpecToHost(host, "allScoresTab", R.string.str_All_Scores, R.id.ScrollViewAllScores);
        
        populateTable(R.id.TableLayout_AllScores, R.xml.scores_all);
        populateTable(R.id.TableLayout_LocalScores, R.xml.scores_local);
        
        Bundle bundle = getIntent().getExtras();
        int initialTab = bundle.getInt(getResources().getString(R.string.bundle_property_initial_tab));
        
        host.setCurrentTab(initialTab);
    }

	private void addTabSpecToHost(TabHost host, String tag, int labelId, int contentViewId)
	{
        TabSpec newTabSpec = host.newTabSpec(tag);
        newTabSpec.setIndicator(getResources().getString(labelId));
        newTabSpec.setContent(contentViewId);
        host.addTab(newTabSpec);	
	}
	
	private void populateTable(int tableId, int XmlId)
	{
        TableLayout allScoresTable = (TableLayout) findViewById(tableId);
        insertHeaderRow(allScoresTable);
        XmlResourceParser allScores = getResources().getXml(XmlId);
        processScores(allScoresTable, allScores);		
	}
	
    private void addTextToRowWithValues(final TableRow tableRow, String text, int textColor, float textSize) 
    {
        TextView textView = new TextView(this);
        textView.setTextSize(textSize);
        textView.setTextColor(textColor);
        textView.setText(text);
        tableRow.addView(textView);
    }
    
    // Create table header row
    private void insertHeaderRow(final TableLayout scoreTable) 
    {
        final TableRow headerRow = new TableRow(this);
        int textColor = getResources().getColor(R.color.scores_table_title);
        float textSize = getResources().getDimension(R.dimen.help_text_size);
        String headerUserName = getResources().getString(R.string.str_Username);
        String headerValue = getResources().getString(R.string.str_Score);
        String headerRank = getResources().getString(R.string.str_Ranking);
        insertRow(scoreTable, textColor, textSize, headerUserName, headerValue, headerRank);
        scoreTable.addView(headerRow);
    }
    
    // Create table scores row
    private void insertScoreRow(final TableLayout scoreTable, String scoreValue, String scoreRank, String scoreUserName) 
    {
        final TableRow newRow = new TableRow(this);
        int textColor = getResources().getColor(R.color.scores_table_text);
        float textSize = getResources().getDimension(R.dimen.help_text_size);
        insertRow(scoreTable, textColor, textSize, scoreUserName, scoreValue, scoreRank);
        scoreTable.addView(newRow);
    }

    private void insertRow(final TableLayout table, int textColor, float textSize, 
    							String column1, String column2, String column3)
    {
        final TableRow newRow = new TableRow(this);
        addTextToRowWithValues(newRow, column1, textColor, textSize);
        addTextToRowWithValues(newRow, column2, textColor, textSize);
        addTextToRowWithValues(newRow, column3, textColor, textSize);
        table.addView(newRow);   	
    }
    
    // Load score records from XML
    private void processScores(final TableLayout scoreTable, XmlResourceParser scores) 
    {
    	try
    	{
	        int eventType = -1;
	        boolean bFoundScores = false;
	        
	        while (eventType != XmlResourceParser.END_DOCUMENT) 
	        {
	            if (eventType == XmlResourceParser.START_TAG) 
	            {
	                // Get the name of the tag (eg scores or score)
	                String strName = scores.getName();
	                if (strName.equals("score")) 
	                {
	                    bFoundScores = true;
	                    String scoreValue = scores.getAttributeValue(null, "score");
	                    String scoreRank = scores.getAttributeValue(null, "rank");
	                    String scoreUserName = scores.getAttributeValue(null, "username");
	                    insertScoreRow(scoreTable, scoreValue, scoreRank, scoreUserName);
	                }
	            }
	            
	            eventType = scores.next();
	        }
	        
	        scores.close();
	
	        if (bFoundScores == false) // no scores available
	        {
	            final TableRow newRow = new TableRow(this);
	            TextView noResults = new TextView(this);
	            noResults.setText(getResources().getString(R.string.str_No_Scores));
	            newRow.addView(noResults);
	            scoreTable.addView(newRow);
	        }
    	}
    	catch(Exception e)
    	{
    		// TODO: notify user via UI
    	}
    }
}